package com.mr.cai.main.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.cai.mr.lib.utils.LogInfo;
import com.cai.mr.lib.utils.NetWorkTypeUtils;
import com.mr.cai.main.MrCaiApplication;
import com.mr.cai.main.R;
import com.mr.cai.main.activity.base.MrCaiBaseActivity;
import com.mr.cai.main.adapter.FavouriteAdapter;
import com.mr.cai.main.bean.CartInfoBean;
import com.mr.cai.main.utils.CommonCoreUtil;
import com.mr.cai.main.view.PublicLoadLayout;
import com.mr.cai.main.view.xlistview.XListView;

import java.util.ArrayList;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by hai8108 on 16/2/25.
 */
public class MrCaiFavouriteActivity extends MrCaiBaseActivity {
    private XListView xListView;
    private FavouriteAdapter adapter;
    private PublicLoadLayout mPublicLayout;
    private ArrayList<CartInfoBean> cartInfoBeans;
    public static void launch(Activity activity){
        Intent intent = new Intent(activity, MrCaiFavouriteActivity.class);
        activity.startActivity(intent);
    }
    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPublicLayout = CommonCoreUtil.createPage(this, R.layout
                .activity_favourite_layout);
        setContentView(mPublicLayout);
        findView();
        initData();
    }
    private void findView(){
        CommonCoreUtil.setClickAction(findViewById(R.id.top_view).findViewById(R.id.pub_top_left)
                , new Action1() {
            @Override
            public void call (Object o) {
                finish();
            }
        });
        ((TextView)findViewById(R.id.top_view).findViewById(R.id.pub_top_mid)).setText(R.string.my_sum_favourite_txt);
        xListView = (XListView) mPublicLayout.findViewById(R.id.favourite_xlist);
        xListView.setScrollable(true);
        xListView.setPullLoadEnable(false);
        xListView.setXListViewListener(ixListViewListener);
        adapter = new FavouriteAdapter(this);
        xListView.setAdapter(adapter);
    }
    private void initData(){
        requestCartData(false, true);
    }
    private XListView.IXListViewListener ixListViewListener = new XListView.IXListViewListener() {
        @Override
        public void onRefresh(XListView view) {
            if (!NetWorkTypeUtils.isNetAvailable(MrCaiApplication.getInstance())) {
                requestCartData(true, false);
            }else{
                xListView.stopRefresh();
                CommonCoreUtil.showToast(R.string.public_loading_net_null_err_txt);
            }
        }
        @Override
        public void onLoadMore(XListView view) {

        }
    };

    private void requestCartData(final boolean isRefresh, final boolean showLoading){
        Observable.just("").subscribeOn(Schedulers.io()).map(new Func1<String,
                ArrayList<CartInfoBean>>() {
            @Override
            public ArrayList<CartInfoBean> call (String s) {
                cartInfoBeans = new ArrayList<CartInfoBean>();
                CartInfoBean mCartInfoBean = null;
                for (int i = 0; i < 10; i++) {
                    mCartInfoBean = new CartInfoBean();
                    mCartInfoBean.setName("菜先生---" + i);
                    mCartInfoBean.setStockNo((i + 340) + "g");
                    mCartInfoBean.setPresentPrice("$" + (i + 38));
                    mCartInfoBean.setCount(i + 1);
                    mCartInfoBean.setChecked(true);
                    cartInfoBeans.add(mCartInfoBean);
                }
                return cartInfoBeans;
            }
        }).doOnSubscribe(new Action0() {
            @Override
            public void call () {
                if (showLoading) {
                    mPublicLayout.loading(true);
                }
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<CartInfoBean>>() {
                    @Override
                    public void call (ArrayList<CartInfoBean> cartInfoBeans) {
                        mPublicLayout.finish();
                        xListView.stopLoadMore();
                        xListView.stopRefresh();

                        adapter.setList(cartInfoBeans);
                        LogInfo.log("haitian", "groupInfoBeans.size =" + cartInfoBeans.size());
                    }
                });
    }
}
